package editor;

import game.Livello;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

public class PannelloAnteprima extends JPanel implements Runnable {
	DialogAnteprima antep;
	Editor edit;
	int centroSchermo;
	
	public PannelloAnteprima(DialogAnteprima antep){
		super();
		this.antep = antep;
		this.edit = antep.getEditor();
		centroSchermo = 400;
		new Thread(this).start();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Image background = antep.getEditor().getImmagineSfondo();
		if(background != null) {
	    	int iniziale = (400 *
	    	    (antep.getWidth() - background.getWidth(null)) /
	    	    (antep.getWidth()- edit.getWidth() * Livello.TILE_SIZE));
	    	int backgroundX = iniziale+(centroSchermo *
	    			(antep.getWidth() - background.getWidth(null)) /
	    			(antep.getWidth()- edit.getWidth() * Livello.TILE_SIZE))*-1; 
	    	g.drawImage(background,backgroundX,0,null);
	    }
		else{
			g.setColor(antep.getEditor().getSfondoEditor());
			g.fillRect(0, 0, (antep.getEditor().getWidth())*Livello.TILE_SIZE, (antep.getEditor().getHeight())*Livello.TILE_SIZE);
		}
		for (SpriteImage imm:edit.getImages()){
				g.drawImage(imm.getImmagine(), imm.getX()- (centroSchermo-(antep.getWidth()/2)), imm.getY(), null);
		}
	}
		
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			if(centroSchermo < (edit.getWidth()*Livello.TILE_SIZE)-(antep.getWidth()/2)){
				this.centroSchermo+=3;
			}
			this.repaint();
			try {
				Thread.sleep(90);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
